package ${domain}.module.wsys.mgr;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import ${domain}.frame.auth.Token;
import ${domain}.frame.base.ErrorType;
import ${domain}.frame.utils.IDgenerator;
import ${domain}.frame.utils.MapperUtil;
import ${domain}.frame.utils.Message;
import ${domain}.frame.utils.ValidationUtil;
import ${domain}.module.wsys.ent.Logtask;
import ${domain}.module.wsys.mpr.LogtaskMapper;
import ${domain}.module.wsys.req.LogtaskCreateRequest;
import ${domain}.module.wsys.req.LogtaskDeleteRequest;
import ${domain}.module.wsys.req.LogtaskFindRequest;
import ${domain}.module.wsys.rsp.LogtaskCreateResponse;
import ${domain}.module.wsys.rsp.LogtaskDeleteResponse;
import ${domain}.module.wsys.rsp.LogtaskFindResponse;

/**
 * LOGTASK - 任务日志
 *
 * @author wangbing
 * @version 0.0.1
 * @since 2020-09-04
 */
@Transactional
@Service
public class LogtaskManagerImpl implements LogtaskManager {

    @Autowired
    private LogtaskMapper logtaskMapper;

    /**
     * 插入
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public LogtaskCreateResponse create(LogtaskCreateRequest request, Token token) {
        LogtaskCreateResponse response = new LogtaskCreateResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long id = IDgenerator.nextId();
        Logtask entity = MapperUtil.map(request, Logtask.class);
        entity.setId(id);

        long result = logtaskMapper.insert(entity, token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.CREATE_FAILURE);
            return response;
        }
        response.setId(id);

        return response;
    }

    /**
     * 逻辑删除
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    public LogtaskDeleteResponse delete(LogtaskDeleteRequest request, Token token) {
        LogtaskDeleteResponse response = new LogtaskDeleteResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        long result = logtaskMapper.delete(request.getId(), token);
        if (1L != result) {
            response.addError(ErrorType.BUSINESS_ERROR, Message.DELETE_FAILURE);
            return response;
        }
        response.setResult(result);

        return response;
    }

    /**
     * 查询
     *
     * @param request 请求对象
     * @param token   令牌
     * @return 响应
     */
    @Transactional(readOnly = true)
    public LogtaskFindResponse find(LogtaskFindRequest request, Token token) {
        LogtaskFindResponse response = new LogtaskFindResponse();

        ValidationUtil.validate(request, response);
        if (response.hasError()) {
            return response;
        }

        if (request.getPageSize() != 0) {
            PageHelper.startPage(request.getPageNumber(), request.getPageSize());
        }
        if (StringUtil.isNotEmpty(request.getSortKey())) {
            PageHelper.orderBy(request.getSortKey() + " " + request.getSortType());
        }
        PageInfo<Logtask> pageInfo = new PageInfo<>(logtaskMapper.find(request, token));

        response.setResult(pageInfo.getList());
        response.setTotalCount(pageInfo.getTotal());

        return response;
    }
}
